/*
 * @Author: your name
 * @Date: 2022-01-05 12:39:56
 * @LastEditTime: 2022-01-05 13:30:46
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /socket/reback/client.cpp
 */
#include <stdio.h>  // printf
#include <string.h> // memset
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h> //socket
#include <netinet/in.h> // IPPROTO_TCP, sockaddr_in

const int BUFFER_SIZE = 100;

int main()
{
    // socket ctor
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // send request 2 server
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = htons(1234);
    connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); // connet instead of bind

    //get the string that will send 2 server
    char bufSend[BUFFER_SIZE] = {0};
    printf("Input a string to send\n");
    fgets(bufSend,BUFFER_SIZE,stdin);
    send(sock,bufSend,strlen(bufSend),0);

    // read data from server
    char bufRecv[BUFFER_SIZE] = {0};
    // read(sock, buffer, sizeof(buffer) - 1);
    recv(sock,bufRecv,BUFFER_SIZE,0);
    printf("message from server:%s\n", bufRecv);

    // close socket
    close(sock);
    return 0;
}